﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Globalization;

namespace OpenAPI.CommonLib
{

	public class SHAEncryption
	{
		public static string Encrypt(string source)
		{
			byte[] StrRes = Encoding.Default.GetBytes(source);
			HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
			StrRes = iSHA.ComputeHash(StrRes);
			StringBuilder EnText = new StringBuilder();
			foreach (byte iByte in StrRes)
			{
				EnText.AppendFormat("{0:x2}", iByte);
			}
			return EnText.ToString();
		}

		public void Test()
		{
			//baidu.zhixin.gethotelcomments&a936003d-6acf-4a3f-ba3a-cb6b0bb8d35c&da1c9045ea4e91cf5c00f00f7f184366&2013-7-23 16:56
			string module = "Baidu";
			string category = "zhixin";
			string methodName = "GetHotelMinPrice";
			string fullActionName = string.Format("{0}.{1}.{2}", module, category, methodName).ToLower();
			string accountId = "1CC9F000-48BC-43FF-A5FE-46EDD143B3D1";
			string password = "19229f81bf086686";
			string token = string.Format("{0}&{1}&{2}&{3}", fullActionName, accountId, password, "2013-7-23 19:00").ToLower(); ;
			string digitalSign = SHAEncryption.Encrypt(token);
			Console.WriteLine(digitalSign);
		}
	}



	/// <summary>
	/// AES 加密类
	/// </summary>
	public class AESEncryption
	{
		#region AES ECB模式算法
		/// <summary>
		/// AES加密 ECB模式算法
		/// </summary>
		/// <param name="toEncrypt">需要加密的原文</param>
		/// <param name="key">加密的密钥（128位，196位，256位）</param>
		/// <returns>加密的密文</returns>
		public static byte[] ECBEncrypt(string toEncrypt, string key)
		{
			byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
			byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
			RijndaelManaged rDel = new RijndaelManaged();
			rDel.KeySize = 128;
			rDel.Key = keyArray;
			rDel.Mode = CipherMode.ECB;
			rDel.Padding = PaddingMode.Zeros;
			ICryptoTransform cTransform = rDel.CreateEncryptor();
			return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
		}

		/// <summary>
		/// AES加密 ECB模式算法
		/// 转Base64
		/// </summary>  
		/// <param name="plainText">需要加密的密文</param>
		/// <param name="key"></param>
		/// <returns></returns>
		public static string ECBEncriptyToBase64(string plainText, string key)
		{
			byte[] resultArray = ECBEncrypt(plainText, key);
			return Convert.ToBase64String(resultArray, 0, resultArray.Length);
		}

		/// <summary>
		/// AES解密 ECB模式算法
		/// </summary>
		/// <param name="toDecrypt">需要解密的密文</param>
		/// <param name="key">解密的密钥（128位，196位，256位）</param>
		/// <returns>解密的原文</returns>
		public static string ECBDecrypt(string toDecrypt, string key)
		{
			byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
			byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
			RijndaelManaged rDel = new RijndaelManaged();
			rDel.KeySize = 128;
			rDel.Key = keyArray;
			rDel.Mode = CipherMode.ECB;
			rDel.Padding = PaddingMode.Zeros;
			ICryptoTransform cTransform = rDel.CreateDecryptor();
			byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
			return UTF8Encoding.UTF8.GetString(resultArray);
		}

		/// <summary>
		/// AES解密 ECB模式算法by PKCS7
		/// </summary>
		/// <param name="toDecrypt">需要解密的密文</param>
		/// <param name="key">解密的密钥（128位，196位，256位）</param>
		/// <returns>解密的原文</returns>
		public static string ECBDecryptByPKC(string toDecrypt, string key)
		{
			byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
			byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

			RijndaelManaged rDel = new RijndaelManaged();
			rDel.Key = keyArray;
			rDel.Mode = CipherMode.ECB;
			rDel.Padding = PaddingMode.PKCS7;

			ICryptoTransform cTransform = rDel.CreateDecryptor();
			byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

			return UTF8Encoding.UTF8.GetString(resultArray);
		}

		public static string ECBEncryptByPKC(string toEncrypt, string key)
		{
			byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
			byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
			RijndaelManaged rDel = new RijndaelManaged();
			rDel.KeySize = 128;
			rDel.Key = keyArray;
			rDel.Mode = CipherMode.ECB;
			rDel.Padding = PaddingMode.PKCS7;
			ICryptoTransform cTransform = rDel.CreateEncryptor();
			var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
			return Convert.ToBase64String(resultArray, 0, resultArray.Length);
		}
		#endregion

		#region 测试
		public static string ECBEncryptByPKCS(string toEncrypt, string key)
		{
			string ekey = key.Substring(0, 32);
			return ECBEncryptByPKC(toEncrypt, ekey);
		}
		public static string ECBDecryptByPKCS(string toDecrypt, string key)
		{
			string dkey = key.Substring(0, 32);
			return ECBDecryptByPKC(toDecrypt, dkey);
		}
		#endregion

		public static string ECBDecrypts(string toDecrypt, string key)
		{
			byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
			byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
			RijndaelManaged rDel = new RijndaelManaged();
			rDel.KeySize = 128;
			rDel.Key = keyArray;
			rDel.Mode = CipherMode.ECB;
			rDel.Padding = PaddingMode.Zeros;
			ICryptoTransform cTransform = rDel.CreateDecryptor();
			byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
			return UTF8Encoding.UTF8.GetString(resultArray);
		}


		void tests()
		{
			string key = "f90072fcea1405b484789ea62f2f2234df85498e";

			Console.WriteLine("\"creditCardInfo\":{");
			Console.WriteLine("\"cardType\":\"" + ECBEncryptByPKCS("中国银行信用卡", key) + "\",");
			Console.WriteLine("\"cardNo\":\"" + ECBEncryptByPKCS("11212332", key) + "\",");
			Console.WriteLine("\"valiCode\":\"" + ECBEncryptByPKCS("112345", key) + "\",");
			Console.WriteLine("\"periodDate\":\"" + ECBEncryptByPKCS("2015-12-31", key) + "\",");
			Console.WriteLine("\"masterName\":\"" + ECBEncryptByPKCS("cgyqu", key) + "\",");
			Console.WriteLine("\"certType\":\"" + ECBEncryptByPKCS("身份证", key) + "\",");
			Console.WriteLine("\"certNo\":\"" + ECBEncryptByPKCS("320321199012242638", key) + "\",");
			Console.WriteLine("\"mobile\":\"" + ECBEncryptByPKCS("15262912222", key) + "\",");
			Console.WriteLine("\"isShow\":" + 0);
			Console.WriteLine("}");
			Console.WriteLine();
			string es = ECBEncryptByPKCS("中国银行信用卡", key);
			Console.WriteLine(es);
			Console.WriteLine(ECBDecryptByPKCS(es, key));
		}

		void test()
		{
			//<cardNumber>tuFZQHdj2mWqKdsD0zvpe7Awka7krzdRMYUVHiqHclo=</cardNumber>
			//<cardType>wkFq72LhjA7HVJ3GHH61Wg==</cardType>
			//<valiCode>4xFR/FMKwgC8ryk2dPRxdg==</valiCode>
			//<masterName>Lm90BVxUFLt0JnmCotyUtg==</masterName>
			//<periodDate>V1ime0pVFONymADt2MBE0g==</periodDate>
			//<certificatesType>89RM/Tik/jlSIKsXrymtsA==</certificatesType>
			//<certificatesNumber>DBdJMLS0thlIVosIT0YAIE0cmARXz290TP4wnLoqOP0=</certificatesNumber>

			string d = "b824b85f37814a6387448a9e3f76f759";
			Console.WriteLine(ECBDecryptByPKC("9FJR0q5fpqZoyX30fymJmT57yAVV68RvXSSElaJ9cyM=", d) + "---信用卡");
			Console.WriteLine(ECBDecryptByPKC("7X4OWbO6ld+NTWzFR4vPUmphncvkbKLK8J/lw+2RUT4=", d) + "---信用卡类型");
			Console.WriteLine(ECBDecryptByPKC("aEhrd7NY2niob/ts3HWOHw==", d) + "---验证码");
			Console.WriteLine(ECBDecryptByPKC("q/mzkiePNq8D0Kt+AxtrPg==", d) + "---持卡人");
			Console.WriteLine(ECBDecryptByPKC("DsZ0NLrXFrb+HCRJIYSS+Q==", d) + "---有效期");
			Console.WriteLine(ECBDecryptByPKC("EZy205FIsbAigSTlBw1MgQ==", d) + "---证件类型");
			Console.WriteLine(ECBDecryptByPKC("cTKWqIqERgFgGc52CrLOeJZCcrW00SLWH1nMCrZii1A=", d) + "---证件号");

			Console.WriteLine(ECBDecryptByPKC("DBdJMLS0thlIVosIT0YAIE0cmARXz290TP4wnLoqOP0=", "52c5e78db000a553a98825c16dcb4516"));


			Console.WriteLine("-----------------");
			Console.WriteLine("<cardNumber>" + ECBEncryptByPKC("4432260017325612", d) + "</cardNumber>");
			Console.WriteLine("<cardType>" + ECBEncryptByPKC("中信银行信用卡", d) + "</cardType>");
			//Console.WriteLine("<cardNumber>" + ECBEncryptByPKC("3568891104058409", d) + "</cardNumber>");
			//Console.WriteLine("<cardType>" + ECBEncryptByPKC("招商银行信用卡", d) + "</cardType>");

			//3568891104058409 
			//招商银行信用卡

			Console.WriteLine("<valiCode>" + ECBEncryptByPKC("866", d) + "</valiCode>");
			Console.WriteLine("<masterName>" + ECBEncryptByPKC("叶xx", d) + "</masterName>");
			Console.WriteLine("<periodDate>" + ECBEncryptByPKC("2016-1-1", d) + "</periodDate>");
			Console.WriteLine("<certificatesType>" + ECBEncryptByPKC("身份证", d) + "</certificatesType>");
			Console.WriteLine("<certificatesNumber>" + ECBEncryptByPKC("320586199003290510", d) + "</certificatesNumber>");
			Console.WriteLine("<masterMobileNumber>" + ECBEncryptByPKC("18605121225", d) + "</masterMobileNumber>");

			Console.WriteLine("-----------------");

			Console.WriteLine("<cardholderName>" + ECBEncryptByPKC("汤晓华", d) + "</cardholderName>");
			Console.WriteLine("<cardNumber>" + ECBEncryptByPKC("4013992278833442", d) + "</cardNumber>");
			Console.WriteLine("<cardType>" + ECBEncryptByPKC("中信银行信用卡", d) + "</cardType>");
			Console.WriteLine("<validYear>" + ECBEncryptByPKC("2013", d) + "</validYear>");
			Console.WriteLine("<validMonth>" + ECBEncryptByPKC("9", d) + "</validMonth>");
			Console.WriteLine("<verifyCode>" + ECBEncryptByPKC("291", d) + "</verifyCode>");
			Console.WriteLine("<certificateType>" + ECBEncryptByPKC("身份证", d) + "</certificateType>");
			Console.WriteLine("<certificateNumber>" + ECBEncryptByPKC("220821198609260025", d) + "</certificateNumber>");


			//<cardholderName>祁济</cardholderName>
			//<cardNumber>5203823130018954</cardNumber>
			//<cardType>广东发展银行信用卡</cardType>
			//<validYear>2013</validYear>
			//<validMonth>9</validMonth>
			//<verifyCode>291</verifyCode>
			//<certificateType>身份证</certificateType>
			//<certificateNumber>220821198609260025</certificateNumber>


		}

	}

}
